package com.gw.quotes.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.activity.QuoteProductDetailActivity;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem.ProjectGood.Projectaccessorie;

public class QuoteProjectaccessorieAdapter extends BaseAdapter {

	private List<Projectaccessorie> list;
	private ProjectItem projectItem;
	private Context context;
	private int resource;

	public QuoteProjectaccessorieAdapter(Context context, int layoutId,
			ProjectItem projectItem) {
		this.context = context;
		this.resource = layoutId;
		this.projectItem = projectItem;
		this.list = projectItem.getProjectGoodList().get(0)
				.getProjectaccessorieList();
	}

	@Override
	public int getCount() {
		return this.list.size() + 1;
	}

	@Override
	public Object getItem(int position) {
		if (position == 0) {
			return projectItem.getProjectGoodList().get(0);
		}
		return list.get(position - 1);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout layout = null;
		if (convertView == null) {
			layout = (LinearLayout) LayoutInflater.from(this.context).inflate(
					resource, null);
		} else {
			layout = (LinearLayout) convertView;
		}
		TextView goods = (TextView) layout.findViewById(R.id.goods_text);
		TextView number = (TextView) layout.findViewById(R.id.number_text);
		TextView price = (TextView) layout.findViewById(R.id.unit_price_text);
		if (position == 0) {
			goods.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							QuoteProductDetailActivity.class);
					intent.putExtra("itemInfo", projectItem);
					context.startActivity(intent);
				}
			});
			goods.setText(projectItem.getProjectGoodList().get(0)
					.getGoodsname());
			number.setText(projectItem.getQty() + "");
			price.setText(String.format("￥%.2f", projectItem
					.getProjectGoodList().get(0).getAgreementprice()));
			;
		} else {
			Projectaccessorie projectaccessorie = list.get(position - 1);
			goods.setText(projectaccessorie.getGoodsname());
			number.setText(projectaccessorie.getQty() + "");
			price.setText(String.format("￥%.2f",
					projectaccessorie.getAgreementprice()));
		}
		return layout;
	}
}
